<?php
namespace app\api\controller;
use think\Request;
use app\base\model\{
    GoodsCate,
    MasterVideo,
    Goods,
    Shipment,
    NavMenu,
    AccessLog,
    SlideCate,
    Slide,
    CustomServer,
    IndexModule
};
class Index extends Base
{
    //商品分类模型
    protected $cate;
    //大师视频模型
    protected $masterVideo;
    //商品模型
    protected $goods;
    //导航模型
    protected $nav;
    //访问记录模型
    protected $access_log;
    //客服模型
    protected $custom_server;
    public function __construct(Request $request = null)
    {
        parent::__construct($request);
        $this->cate    =  new GoodsCate();
        $this->masterVideo = new MasterVideo();
        $this->goods       = new Goods();
        $this->nav         = new NavMenu();
        $this->access_log  = new AccessLog();
        $this->custom_server = new CustomServer();
    }
    /**
     *商品分类列表
     * return json
     */
    public function getCateList(){
       $parent_id = $this->request->post('parent_id',0);
       if(empty($parent_id) || !isset($parent_id)){
           $cate_list=$this->cate->where(['level'=>2,'is_show'=>1])->order('cate_id','desc')->select();
       }else{
           $cate_list=$this->cate->where(['pid'=>$parent_id,'is_show'=>1])->order('cate_id','desc')->select();
       }
       if(!empty($cate_list)){
           $msg['message']=checkMsg(GET_DATA_SUCCESS);
           $msg['cate_list']=$cate_list;
       }else{
           $msg['message']=checkMsg(GET_DATA_FAILE);
       }
       return $msg;
    }
   /**
    *大师视频列表
    * return json
    */
  public function getVideoList(){
      $where=[
          'status' =>1,
          'is_delete' =>0
      ];
      $videoList=$this->masterVideo->where($where)->order('v_id','desc')->limit(7)->select();
      if(!empty($videoList)){
          $msg['message']=checkMsg(GET_DATA_SUCCESS);
          $msg['list']=$videoList;
      }else{
          $msg['message']=checkMsg(GET_DATA_FAILE);
      }
      return $msg;
  }
  /**
   *每日一运
   * return json
   */
  public function shipment(){
      $shipment_model=new Shipment();
      $info=$shipment_model->find();
      if(!empty($info)){
          $msg['message']=checkMsg(GET_DATA_SUCCESS);
          $msg['info']=$info;
      }else{
          $msg['message']=checkMsg(GET_DATA_FAILE);
      }
      return $msg;
  }
  /**
   *左侧导航栏
   * return json
   */
   public function leftNavitor(){
       $list=['今日特购','热门商品','大师推荐'];
       $cateList=$this->cate->alias('c')->where(['c.is_show'=>1,'c.is_show_index_module'=>1,'c.pid'=>0])->select();
       foreach($cateList as $key=>$value){
           array_push($list,$value['cate_name']);
       }
      $msg['message']=checkMsg(GET_DATA_SUCCESS);
      $msg['navitor_list']=$list;
      return $msg;
   }
   /**
    *首页导航栏
    * return json
    */
   public function getNavList(){
       $type=$this->request->post('type',1);  //1:顶部操作栏 2:导航栏 3:功能导航栏 4:右侧功能栏 5:底部操作栏 6:版权栏
       $list=[];
       $top_list=$this->nav->where(['status'=>1,'parent_id'=>0,'nav_id'=>1])->select(); //顶部导航栏
       foreach($top_list as $key=>$value){
          $child_list=$this->nav->where(['parent_id'=>$value['id'],'status'=>1,'parent_id'=>$value['id']])->select();
          if(empty($child_list)){
              $top_list[$key]['child_list']=[];
          }else{
              $top_list[$key]['child_list']=$child_list;
          }
       }
       $nav_list = $this->nav->where(['status'=>1,'parent_id'=>0,'nav_id'=>2])->select(); //导航栏
       foreach($nav_list as $key=>$value){
           $child_list=$this->nav->where(['parent_id'=>$value['id'],'status'=>1,'parent_id'=>$value['id']])->select();
           if(empty($child_list)){
               $nav_list[$key]['child_list']=[];
           }else{
               $nav_list[$key]['child_list']=$child_list;
           }
       }
       $func_list = $this->nav->where(['status'=>1,'parent_id'=>0,'nav_id'=>3])->select(); //功能导航栏
       foreach($func_list as $key=>$value){
           $child_list=$this->nav->where(['parent_id'=>$value['id'],'status'=>1,'parent_id'=>$value['id']])->select();
           if(empty($child_list)){
               $func_list[$key]['child_list']=[];
           }else{
               $func_list[$key]['child_list']=$child_list;
           }
       }
       $right_list = $this->nav->where(['status'=>1,'parent_id'=>0,'nav_id'=>4])->select(); //右侧导航栏
       foreach($right_list as $key=>$value){
           $child_list=$this->nav->where(['parent_id'=>$value['id'],'status'=>1,'parent_id'=>$value['id']])->select();
           if(empty($child_list)){
               $right_list[$key]['child_list']=[];
           }else{
               $right_list[$key]['child_list']=$child_list;
           }
       }
       $bottom_list = $this->nav->where(['status'=>1,'parent_id'=>0,'nav_id'=>5])->select(); //底部导航栏
       foreach($bottom_list as $key=>$value){
           $child_list=$this->nav->where(['parent_id'=>$value['id'],'status'=>1,'parent_id'=>$value['id']])->select();
           if(empty($child_list)){
               $bottom_list[$key]['child_list']=[];
           }else{
               $bottom_list[$key]['child_list']=$child_list;
           }
       }
       $copy_list = $this->nav->where(['status'=>1,'parent_id'=>0,'nav_id'=>6])->select(); //版权导航栏
       foreach($copy_list as $key=>$value){
           $child_list=$this->nav->where(['parent_id'=>$value['id'],'status'=>1,'parent_id'=>$value['id']])->select();
           if(empty($child_list)){
               $copy_list[$key]['child_list']=[];
           }else{
               $copy_list[$key]['child_list']=$child_list;
           }
       }
       $list['top_nav']=$top_list;
       $list['web_nav']=$nav_list;
       $list['func_nav']=$func_list;
       $list['right_nav']=$right_list;
       $list['bottom_nav']=$bottom_list;
       $list['copy_nav']=$copy_list;
       $ip=$_SERVER['REMOTE_ADDR'];
       $date=date('Y-m-d',time());
       $accessInfo=$this->access_log->where(['log_ip'=>$ip,'date'=>$date])->find();
       if(empty($accessInfo)){
           $log_data=[
               'log_ip'  =>$ip,
               'date'    =>$date,
               'count'   =>1,
               'add_time'=>time()
           ];
           $this->access_log->insertGetId($log_data);
       }else{
           $save_data=[
               'count' =>$accessInfo['count']+1
           ];
           $this->access_log->save($save_data,['log_ip'=>$ip,'date'=>$date]);
       }
       if(!empty($list)){
           $msg['message']=checkMsg(GET_DATA_SUCCESS);
           $msg['list']=$list;
       }else{
           $msg['message']=checkMsg(GET_DATA_FAILE);
       }
       return $msg;
   }
  /**
   *联系客服
   * return json
   */
 public function getCustomServer(){
     $custom_server=$this->custom_server->select();
     $key=array_rand($custom_server,1);
     $info=$custom_server[$key];
     $module=new IndexModule();
     $public_wechat=$module->where(['m_name'=>'微信公众号'])->find();
     $info['public_wechat']=$public_wechat['banner_img'];
     $msg['message']=checkMsg(GET_DATA_SUCCESS);
     $msg['custom_server']=$info;
     return $msg;
 }
   /**
    *轮播图列表
    *return json
    */
   public function getSlideList(){
      $slideModel = new Slide();
      $cate_id = $this->request->post('cate_id',1); //1:首页 2:商品 3:大师
      $slide_list=$slideModel->where(['cate_id'=>$cate_id,'is_play'=>1])->select();
      if(!empty($slide_list)){
          $msg['message']=checkMsg(GET_DATA_SUCCESS);
          $msg['list']=$slide_list;
      }else{
          $msg['message']=checkMsg(GET_DATA_FAILE);
      }
      return $msg;
   }

    /**
     *上传图片
     * return json
     */
    public function uploadImg(){
        $checkData=array(
            'size'  =>'15678000',
            'ext'   =>'jpg,png,gif'
        );
        return $this->upload($checkData);
    }
    /**
     *获取某一菜单栏下的内容
     * return json
     */
   public function getNavContent(){
       $nav_id = $this->request->post('nav_id');
       if(empty($nav_id)){
           $msg['message']=checkMsg(CANSHU_ERROR);
           return $msg;
       }
       $navInfo=$this->nav->where(['id'=>$nav_id,'status'=>1])->find();
       if(empty($navInfo)){
           $msg['message']=checkMsg(NAV_NOT_EXITS);
           return $msg;
       }
       $child_nav=$this->nav->where(['parent_id'=>$nav_id,'status'=>1])->field('name,text')->select();
       $navInfo['child_nav_list']=$child_nav;
       $msg['message']=checkMsg(GET_DATA_SUCCESS);
       $msg['info']=$navInfo;
       return $msg;
   }
}
